<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="年级">
              <a-select  :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsnd">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
           <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" >
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+"-"+  i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
           <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="培养层次">
              <a-select  :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.pyccm" >
                <a-select-option v-for="i in TYPE0310" :key="i.id" :value="i.code">{{i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="奖学金类型">
              <a-select  :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.jxjdjdm">
                <a-select-option v-for="i in TYPE_jxjlx" :key="i.id" :value="i.code">{{i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
       <!--   <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="科目类型">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.kmlxm">
                <a-select-option v-for="i in selectMenu.kmlx" :key="i.code" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          </template>
          <a-col :sm="{span:16}" :xl="{span:16}">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="handleCreate">添加</a-button>
            <a-button type="primary" @click="loadData">查询</a-button>
            <!-- <a-button type="primary" @click="exportExcel">导出</a-button> -->
            <a-button type="primary" @click="handleBatchDelete">批量删除</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :dataSource="data"
      :pagination="pagination"
      @change="handleTableChange"
    >
      <template slot="action" slot-scope="text,record">
        <a @click="handleEdit(record.id)">修改</a>
        <a @click="handleDelete(record.id)">删除</a>
      </template>
      <template slot="kmmc" slot-scope="text,record">
        <a @click="handlePreview(record.id)">{{text}}</a>
      </template>

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    <!-- 新增 -->
    <setup-add ref="referenceAdd" @close="handleCreateClose" @success="handleCreateSuccess" :referenceManageAddVisiable="referenceManageAddVisiable"></setup-add>
    <!-- <reference-add ref="referenceAdd" @close="handleCreateClose" @success="handleCreateSuccess" :referenceManageAddVisiable="referenceManageAddVisiable"></reference-add> -->
    <!-- 编辑 -->
    <setup-edit ref="referenceEdit" @close="handleEditClose" @success="handleEditSuccess" :info="referenceManageEditData"  :referenceManageEditVisiable="referenceManageEditVisiable"></setup-edit> 
    <!-- <reference-edit ref="referenceEdit" @close="handleEditClose" @success="handleEditSuccess" :info="referenceManageEditData"  :referenceManageEditVisiable="referenceManageEditVisiable"></reference-edit> -->
    <!-- 详情 -->
    <!-- <reference-detail ref="referenceDetail" :info="preViewData" ></reference-detail> -->
  </a-card>
</template>

<script>
// import moment from 'moment'
// import ReferenceAdd from './referenceAdd'
// import ReferenceEdit from './referenceEdit'
// import ReferenceDetail from '../reference/referenceDetail'
import setupAdd from './setupAdd'
import setupEdit from './setupEdit'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0126', 'TYPE0310', 'TYPE_jxjlx', 'TYPE0072']

export default {
  components: {
    STable,
    setupAdd,
    setupEdit
    // ReferenceAdd,
    // ReferenceEdit,
    // ReferenceDetail
  },
  data () {
    return {
      advanced: true,
      loading: false,
      data: [],
      // 查询参数
      queryParam: {
        kmbm: '',
        kmmc:'',
        ssyxm:'',
        kmlxm:''
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '年级',
          dataIndex: 'zsnd'
        },
        {
          title: '院系',
          dataIndex: 'ssyxmc',
        },
        {
          title: '培养层次',
          dataIndex: 'pyccm'
        },
        {
          title: '奖学金类型',
          dataIndex: 'jxjdjmc'
        },
        {
          title: '奖学金级别',
          dataIndex: 'jxjlxmmc'
        },
        {
          title: '奖学金金额',
          dataIndex: 'jxje'
        },
        {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],
      selectedRowKeys:[],
      selectedRows:[],
      options: {
         alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
         rowSelection: {
           selectedRowKeys: this.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
             console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
             this.selectedRowKeys = selectedRowKeys
             this.selectedRows = selectedRows
           },
           onSelect: (record, selected, selectedRows) => {
             console.log(record, selected, selectedRows)
           },
           onSelectAll: (selected, selectedRows, changeRows) => {
             console.log(selected, selectedRows, changeRows)
           },
         }
       },
      referenceManageAddVisiable: false,
      referenceManageEditVisiable: false,
      selectMenu: {
        yx: [] ,// 院系
        kmlx:[]
      },
      referenceManageEditData: {},
      preViewData: {},
      pagination: {
        current: 1
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0310: state => state.app['TYPE0310'],
      TYPE_jxjlx: state => state.app['TYPE_jxjlx'],
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.ssyxm = res[0].data[0].dwh
        this.$api.base.dictionaryData('TYPE0430').then(res => {
            this.selectMenu.kmlx = res.data
        })
        this.loadData({
          pageSize: 10,
          pageNum: 1
        })
      })
    },
     // 加载数据
    loadData(parameter = {}) {
      if(!parameter.pageNum){
        this.pagination.current = 1
        parameter.pageNum = 1
      }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.student.setupList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data.rows && res.data.rows.length) {
          this.data = res.data.rows;
        }
        this.loading = false;
      })
    },
    // 分页
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination);
      const pager = { ...this.pagination
      };
      pager.current = pagination.current;
      this.pagination = pager;
      this.loadData({
        pageSize: 10,
        pageNum: pagination.current
      });
    },
     // 多选
    onSelectChange (selectedRowKeys,selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
    // 选择院系
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    // 添加
    handleCreate () {
      this.referenceManageAddVisiable = true
    },
    // 关闭添加弹层
    handleCreateClose () {
      this.referenceManageAddVisiable = false
    },
    // 添加成功
    handleCreateSuccess () {
      this.referenceManageAddVisiable = false
      this.$message.success('添加成功')
      this.loadData({
        pageSize:10,
        pageNum:this.pagination.current
      })
    },
    // 查看详情
    handlePreview (key) {
      this.$api.recruitstat.entrySubjectManageId(key).then( res => {
           this.preViewData = res.data
            this.$refs.referenceDetail.show()
      })
    },
    // 修改
    handleEdit (key) {
      this.$api.student.setupId(key).then( res => {
           this.referenceManageEditData = res.data
            this.referenceManageEditVisiable = true
      })
    },
    // 关闭添加弹层
    handleEditClose () {
      this.referenceManageEditVisiable = false
    },
    // 添加成功
    handleEditSuccess () {
      this.referenceManageEditVisiable = false
      this.$message.success('修改成功')
      this.loadData({
        pageSize:10,
        pageNum:this.pagination.current
      })
    },
    // 导出
    // exportExcel () {
    //   console.log('导出')
    //   this.$export('admissions/zsSsckkm/export', this.queryParam)
    // },
    // 删除
    handleDelete (key) {
      console.log('删除')
      var that = this;
      this.$confirm({
        title: '确定删除?',
        onOk() {
          console.log('OK');
          that.deleteData(key)
        },
        onCancel() {
          console.log('Cancel');
        },
        class: 'test',
      });
    },
    // 批量删除
    handleBatchDelete () {
      console.log('批量删除')
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的数据')
        return
      }
      var deleteId
      if (this.selectedRows && this.selectedRows.length){
        deleteId = this.selectedRows.map(v => {
          return v.id
        }).toString()
      }
      console.log(deleteId)
       var that = this;
       this.$confirm({
         title: '确定删除?',
         onOk() {
           console.log('OK');
           that.deleteData(deleteId)
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });
    },
    deleteData(id){
      this.$api.student.setupDel(id).then( res => {
        this.loadData({
          pageSize:10,
          pageNum:this.pagination.current
        })
        this.selectedRowKeys = []
      })
    }
  }
}
</script>
